﻿/* Created by Scott Irons, Vinicios da Silva
 * Contact: scott@passltd.ca, vinicios.dasilva@edu.sait.ca */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TravelExpertsManagementConsole
{
    public partial class formSupplierContact : Form
    {
        private int supplierId;

        public formSupplierContact(frmContainer parent, int sid)
        {
            InitializeComponent();

            MdiParent = parent;
            supplierId = sid;
        }

        private void SupplierContact_FormClosing(object sender, FormClosingEventArgs e)
        {
            frmContainer parent = (frmContainer)MdiParent;

            parent.childSupplierContact = null;
        }

        private void formSupplierContact_Load(object sender, EventArgs e) //everytime it loads refresh the listbox
        {
            refreshTheData(); //call the method for refreshing
        }

        private void lstboxSupplier_SelectedIndexChanged(object sender, EventArgs e)// Display the information
        {
            if (lstboxSupplier.SelectedIndex == -1)
            {
                return;
            }

            SupplierContact SupCon = new SupplierContact(); //creating the conection 
            SupCon = (SupplierContact)lstboxSupplier.SelectedItem;

            //getting dataBase information
            tbContactId.Text = SupCon.SupplierContactId.ToString(); //15
            tbFistName.Text = SupCon.SupConFirstName; //14
            tbLastName.Text = SupCon.SupConLastName; //13
            tbPhone.Text = SupCon.SupConBusPhone; //12
            tbFax.Text = SupCon.SupConFax; //11
            tbEmail.Text = SupCon.SupConEmail; //10
            tbURL.Text = SupCon.SupConURL; //9
            tbCity.Text = SupCon.SupConCity; //8
            tbAddress.Text = SupCon.SupConAddress; //7
            tbProvince.Text = SupCon.SupConProv; //6
            tbPostalCode.Text = SupCon.SupConPostal; //5
            tbAffName.Text = SupCon.AffName; //4
            tbAffId.Text = SupCon.AffiliationId; //3
            tbAffDisc.Text = SupCon.AffDesc; //2
            tbCompanyName.Text = SupCon.SupConCompany; //1
            tbCountry.Text = SupCon.SupConCountry; //16

            //Assign the read Only
            tbAffId.ReadOnly = true;
            tbContactId.ReadOnly = true;
        }

        private void refreshTheData() //refreshing the list
        {
            try
            {
                SupplierContactDataAccess DataBase = new SupplierContactDataAccess();
                lstboxSupplier.DataSource = DataBase.Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private SupplierContact getContact() //to populate the boxes
        {
            SupplierContact SupCon = new SupplierContact();
            SupCon.SupConAddress = tbAddress.Text;
            SupCon.SupConBusPhone = tbContactId.Text;
            SupCon.SupConCity = tbCity.Text;
            SupCon.SupConCompany = tbCompanyName.Text;
            SupCon.SupConCountry = tbCountry.Text;
            SupCon.SupConEmail = tbEmail.Text;
            SupCon.SupConFax = tbFax.Text;
            SupCon.SupConFirstName = tbFistName.Text;
            SupCon.SupConLastName = tbLastName.Text;
            SupCon.SupConPostal = tbPostalCode.Text;
            SupCon.SupConProv = tbProvince.Text;
            SupCon.SupConURL = tbURL.Text;
            SupCon.SupplierContactId = int.Parse(tbContactId.Text);
            SupCon.AffDesc = tbAffDisc.Text;
            SupCon.AffiliationId = tbAffId.Text;
            SupCon.AffName = tbAffName.Text;

            return SupCon;
        }

        private bool FormValidation() //To see if the customer entered anyData
        {
            int idNumeric;

            if (!int.TryParse(tbAffId.Text, out idNumeric))
            {
                MessageBox.Show("Please ensure the Affiliation ID is Numeric!");
            }

            if (!int.TryParse(tbContactId.Text, out idNumeric))
            {
                MessageBox.Show("Please ensure the Contact ID is Numeric!");
            }

            if (!int.TryParse(tbFax.Text, out idNumeric))
            {
                MessageBox.Show("Please ensure the Fax Number is Numeric!");
            }

            if (!int.TryParse(tbPhone.Text, out idNumeric))
            {
                MessageBox.Show("Please ensure the Phone Number is Numeric!");
            }

            if (tbFistName.Text == "")
            {
                MessageBox.Show("Please make sure to enter a First Name!");
            }

            if (tbLastName.Text == "")
            {
                MessageBox.Show("Please make sure to enter a Last Name!");
            }

            if (tbPhone.Text == "")
            {
                MessageBox.Show("Please make sure to enter a Phone Number!");
            }

            if (tbEmail.Text == "")
            {
                MessageBox.Show("Please make sure to enter an Email!");
            }
            return true;
        }

        private void FormClear() // to clear the whole form
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.GetType() == typeof(TextBox))
                {
                    ctrl.Text = "";
                }

                tbContactId.ReadOnly = false;
                tbAffId.ReadOnly = false;
            }
        }

        private void btnNew_Click(object sender, EventArgs e) //This is the New Button
        {
            FormClear(); //Just Call the clear form method
        }

        private void btnAdd_Click(object sender, EventArgs e) //this is the Add button
        {
            if (!FormValidation())
            {
                return;
            }

            try
            {
                SupplierContactDataAccess DataAccess = new SupplierContactDataAccess();
                DataAccess.Adding(getContact());
                refreshTheData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e) //this is the update button
        {
            if (lstboxSupplier.SelectedIndex == -1)
            {
                MessageBox.Show("Please make sure a Contact is Selected!");
                return;
            }

            if (!FormValidation())
            {
                return;
            }

            try
            {
                SupplierContactDataAccess DataAccess = new SupplierContactDataAccess();
                DataAccess.UpDate(getContact());
                refreshTheData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e) //this is the deleting button
        {
            if (lstboxSupplier.SelectedIndex == -1)
            {
                MessageBox.Show("Please make sure a Contact is Selected!");
                return;
            }

            if (MessageBox.Show("Are you sure you want to delete this Contact?", "well deletion", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            try
            {
                SupplierContactDataAccess DataAccess = new SupplierContactDataAccess();
                DataAccess.Delete(getContact());
                refreshTheData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
